﻿using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Helper;

namespace XCI.Service
{
    public class DataFileUploadService
    {
        private const string RootUrl = "/data/file";
        public static DataFileUploadService Instance { get; } = new DataFileUploadService();

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="localFilePath">待上传的本地文件路径</param>
        /// <param name="remoteFilePath">服务器文件路径</param>
        public async Task<FileModel> UploadAsync(string localFilePath, string remoteFilePath)
        {
            return await App.FileClient.UploadAsync<FileModel>($"{RootUrl}/upload", localFilePath,
                new { virtualFilePath = remoteFilePath });
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remoteFilePath">远程文件路径</param>
        /// <param name="localFilePath">文件本地存储路径</param>
        /// <param name="md5">文件md5,可为null</param>
        public Task DownloadAsync(string remoteFilePath, string localFilePath, string md5 = null)
        {
            return App.FileClient.DownloadAsync($"{RootUrl}/download", localFilePath,
                new { virtualFilePath = remoteFilePath, md5 });

        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remoteFilePath">远程文件路径</param>
        /// <param name="md5">文件md5,可为null</param>
        public async Task<byte[]> DownloadAsBytesAsync(string remoteFilePath, string md5 = null)
        {
            string tempFile = Path.GetTempFileName();
            await DownloadAsync(remoteFilePath, tempFile, md5);
            var result = File.Exists(tempFile) ? IOHelper.ReadFile(tempFile) : null;
            IOHelper.DeleteFile(tempFile);
            return result;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="remoteFilePath">远程文件路径</param>
        public Task DeleteAsync(string remoteFilePath)
        {
            return App.FileClient.PostAsync($"{RootUrl}/delete", new { virtualFilePath = remoteFilePath });
        }

        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="srcRemoteFilePath">源文件路径</param>
        /// <param name="destRemoteFilePath">目标文件路径</param>
        public Task CopyAsync(string srcRemoteFilePath, string destRemoteFilePath)
        {
            return App.FileClient.PostAsync($"{RootUrl}/copy",
                new { srcVirtualFilePath = srcRemoteFilePath, destVirtualFilePath = destRemoteFilePath });
        }

        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="remoteFilePath">检查的远程文件路径</param>
        public Task<bool> ExistsAsync(string remoteFilePath)
        {
            return App.FileClient.PostAsync<bool>($"{RootUrl}/exists", new { virtualFilePath = remoteFilePath });
        }

        /// <summary>
        /// 查询文件列表
        /// </summary>
        /// <param name="virtualCatalog">文件虚拟目录</param>
        public async Task<List<FileModel>> ListAsync(string virtualCatalog)
        {
            return await App.FileClient.PostAsync<List<FileModel>>($"{RootUrl}/list", new { virtualCatalog });
        }
    }
}